/*
 * Ext GWT 2.2.4 - Ext for GWT
 * Copyright(c) 2007-2010, Ext JS, LLC.
 * licensing@extjs.com
 * 
 * http://extjs.com/license
 */
package id.co.rho.scoring.cid.client.mvc;

import id.co.rho.scoring.cid.client.security.Credential;

import com.extjs.gxt.ui.client.Registry;
import com.extjs.gxt.ui.client.Style.LayoutRegion;
import com.extjs.gxt.ui.client.mvc.AppEvent;
import com.extjs.gxt.ui.client.mvc.Controller;
import com.extjs.gxt.ui.client.mvc.Dispatcher;
import com.extjs.gxt.ui.client.mvc.View;
import com.extjs.gxt.ui.client.util.Margins;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.HtmlContainer;
import com.extjs.gxt.ui.client.widget.Viewport;
import com.extjs.gxt.ui.client.widget.custom.ThemeSelector;
import com.extjs.gxt.ui.client.widget.form.LabelField;
import com.extjs.gxt.ui.client.widget.layout.BorderLayout;
import com.extjs.gxt.ui.client.widget.layout.BorderLayoutData;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.inject.Inject;

public class AppView extends View {
	public static final String CENTER_PANEL = "centerPanel";
	public static final String WEST_PANEL = "westPanel";
	public static final String NORTH_PANEL = "northPanel";
	public static final String VIEWPORT = "viewPort";

	private Viewport viewport;
	private ContentPanel centerPanel;
	private HtmlContainer northPanel;
	private ContentPanel westPanel;
	private Credential credential;
	LabelField userName;

	@Inject
	public AppView(Credential credential) {
		super(null);
		controller = new ViewController();
		this.credential = credential;
	}

	protected void handleEvent(AppEvent event) {
		AppEvent appEvent = new AppEvent(AppEvents.TestView);
		appEvent.setHistoryEvent(true);
		appEvent.setToken("1");
		Dispatcher.forwardEvent(appEvent);
	}

	protected void initialize() {
		viewport = new Viewport();
		viewport.setLayout(new BorderLayout());
		Registry.register(VIEWPORT, viewport);

		createNorth();
		createWest();
		createCenter();
		RootPanel.get().add(viewport);
	}

	private void createCenter() {
		BorderLayoutData centerData = new BorderLayoutData(LayoutRegion.CENTER);
		centerData.setCollapsible(false);
		centerPanel = new ContentPanel();
		centerPanel.setLayout(new FitLayout());
		centerPanel.setHeaderVisible(false);
		viewport.add(centerPanel, centerData);
		Registry.register(CENTER_PANEL, centerPanel);
	}

	private void createWest() {
		BorderLayoutData westData = new BorderLayoutData(LayoutRegion.WEST,
				200, 150, 300);
		westData.setCollapsible(true);
		westData.setSplit(true);

		westPanel = new ContentPanel();
		westPanel.setHeading("Navigation Panel");
		westPanel.setAnimCollapse(true);
		viewport.add(westPanel, westData);
		westPanel.hide();
		Registry.register(WEST_PANEL, westPanel);
	}

	private void createNorth() {
		StringBuffer sb = new StringBuffer();
		sb.append("<div id='pefindo-header' class='x-small-editor'><div id='pefindo-theme'></div><div id='pefindo-user'></div></div>");
		northPanel = new HtmlContainer(sb.toString());
		northPanel.setStateful(false);

		userName = new LabelField();
		userName.setStyleAttribute("color", "white");
		userName.setWidth(200);
		userName.setHeight(50);
		northPanel.add(userName, "#pefindo-user");

		ThemeSelector selector = new ThemeSelector();
		selector.setWidth(125);
		northPanel.add(selector, "#pefindo-theme");

		BorderLayoutData northData = new BorderLayoutData(LayoutRegion.NORTH,
				40);
		northData.setMargins(new Margins());
		viewport.add(northPanel, northData);
		Registry.register(NORTH_PANEL, northPanel);
	}

	public void setEnabled(boolean b) {
		westPanel.setEnabled(b);
	}

	public LabelField getUserName() {
		return userName;
	}

	private class ViewController extends Controller {
		public ViewController() {

			registerEventTypes(AppEvents.Init);
			registerEventTypes(AppEvents.LoginSuccess);
			registerEventTypes(AppEvents.MainLoginView);
		}

		public void handleEvent(AppEvent event) {
			if (event.getType() == AppEvents.Init) {
				forwardToView(AppView.this, event);
				AppView.this.setEnabled(false);
			} else if (event.getType() == AppEvents.LoginSuccess) {
				AppView.this.setEnabled(true);
				ContentPanel navPanel = (ContentPanel) Registry
						.get(AppView.WEST_PANEL);
				navPanel.show();
				// Dispatcher.forwardEvent(AppEvents.MainSearchView);
				ContentPanel centerPanel = (ContentPanel) Registry
						.get(AppView.CENTER_PANEL);
				centerPanel.removeAll();
				centerPanel.layout();
				AppView.this.getUserName().setText(
						"Welcome " + credential.getUsername());
			} else if (event.getType() == AppEvents.MainLoginView) {
				AppView.this.setEnabled(false);
			}
		}
	}
}
